package com.boe.common.util;


import com.boe.common.jdbc.JsonMapRowMapper;
import com.boe.common.servlet.HealthServlet;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Import;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;


@Import(value = {JsonMapRowMapper.class, HealthServlet.class})
@Order(PriorityOrdered.HIGHEST_PRECEDENCE)
public class SpringUtil implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, ApplicationContextAware {
    private static Environment environment = null;
    private static boolean inited = false;

    public static Environment getEnvironment() {
        return environment;
    }

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        environment = event.getEnvironment();

        if (inited == false) {
            inited = true;
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        environment = applicationContext.getEnvironment();
    }

}
